package models;

import java.io.File;
import java.util.List;
import java.util.regex.Pattern;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;




import java.util.regex.Matcher;

import beans.PageBean;
import utils.Config;
import utils.SessionUtils;
import utils.StringUtils;

@Entity
@Table(name="attachment")
public class Attachment extends BasicModel{
	@Column(name = "attachmentId")
	public String attachmentId;
	
	@Column(name = "url")
	public String url;
	
	public static Attachment addAttachment(String name){
        Attachment attachment = new Attachment();
        attachment.url = Config.attachmentPath + name;
        attachment.save();
        return attachment;
    }
	
	public static void deleteAttachment(String id)
	{
		if(StringUtils.isEmpty(id))
		{
			return ;
		}
		Attachment attachment = Attachment.findById(id);
		if(attachment != null)
		{
			String fileAdd = attachment.url;
			File file = new File(fileAdd);
			if(file.exists())
			{
				file.delete();
			}
			attachment.delete();
		}
	}

}
